package controller.Operation;

import model.Book;
import model.NormalUser;
import model.Record;
import util.BookDao;
import util.NormalUserDao;
import util.RecordDao;

import javax.swing.table.DefaultTableModel;
import java.util.List;

/**
 * @Question Show
 * @Date 2022/12/18 11:54
 * @Solution
 */
public class Show {
    public static void showAllBook(DefaultTableModel bookModel) {
        bookModel.setRowCount(0);
        BookDao bookDao = new BookDao();
        List<Book> bookList = bookDao.getAll();
        for(Book book:bookList) {
            if(book.isState()) {
                Object[] info = {book.getBookId(), book.getBookName(),
                        book.getBookAuthor(), book.getBookPre(), "可借"};
                bookModel.addRow(info);
            } else {
                Object[] info = {book.getBookId(), book.getBookName(),
                        book.getBookAuthor(), book.getBookPre(), "不可借"};
                bookModel.addRow(info);
            }
        }
    }
    public static void showAllUser(DefaultTableModel userModel) {
        userModel.setRowCount(0);
        NormalUserDao normalUserDao = new NormalUserDao();
        List<NormalUser> userList = normalUserDao.getAll();
        for(NormalUser user:userList) {
            Object[] info = {user.getId(),user.getUsername(), user.getPassword()};
            userModel.addRow(info);
        }
    }
    public static void showAllRecord(DefaultTableModel recordModel) {
        recordModel.setRowCount(0);
        RecordDao recordDao = new RecordDao();
        List<Record> recordList = recordDao.getAll();
        for(Record record:recordList) {
            Object[] info = {record.getId(),record.getBookId(),record.getUserId(),record.getOutTime(),record.getInTime()};
            recordModel.addRow(info);
        }
    }
    public static void showByUserIdRecord(DefaultTableModel recordModel,int userId) {
        recordModel.setRowCount(0);
        RecordDao recordDao = new RecordDao();
        List<Record> recordList = recordDao.getByUserId(userId);
        for(Record record:recordList) {
            Object[] info = {record.getId(),record.getBookId(),record.getUserId(),record.getOutTime(),record.getInTime()};
            recordModel.addRow(info);
        }
    }
}
